//
//  Comment.m
//  Echo
//
//  Created by Liao on 14/12/16.
//  Copyright (c) 2014年 sina. All rights reserved.
//

#import "Comment.h"
#import "WeiboApiClient.h"
#import "WeiboApiRobotClient.h"
#import "GTMNSString+URLArguments.h"

@implementation Comment

@synthesize cid = _cid;
@synthesize is_hot = _is_hot;
@synthesize owner = _owner;
@synthesize text = _text;
@synthesize created_time = _created_time;

- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    _cid = [attributes valueForKey:@"id"];
    _is_hot = [NSNumber numberWithInt:1]; // 暂时都设为1
    _owner = [[User alloc] init];
    _owner.uid = [[attributes valueForKey:@"user"] valueForKey:@"idstr"];
    _owner.screen_name = [[attributes valueForKey:@"user"] valueForKey:@"screen_name"];
    _owner.profile_image_url = [[attributes valueForKey:@"user"] valueForKey:@"profile_image_url"];
    _text = [attributes valueForKey:@"text"];
    _created_time = [attributes valueForKey:@"created_at"];
    return self;
}

+ (NSURLSessionDataTask *)commentsOfTweet:(NSString *)mid AtPage:(NSNumber *)page WithBlock:(void (^)(NSArray *comments, NSError *error))block {
    NSDictionary *params = @{@"access_token":[CurrentUser GetToken], @"id":mid, @"page":page, @"count":[NSNumber numberWithInt:HOT_COMMENTS_FETCH_NUM]};
    
    return [[WeiboApiClient sharedClient] GET:WEIBO_API_HOT_COMMENTS parameters:params success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSArray *commentsFromResponse = [JSON valueForKeyPath:@"comments"];
        NSMutableArray *mutableComments = [NSMutableArray arrayWithCapacity:[commentsFromResponse count]];
        for (NSDictionary *attributes in commentsFromResponse) {
            Comment *comment = [[Comment alloc] initWithAttributes:attributes];
            [mutableComments addObject:comment];
        }
        if (block) {
            block([NSArray arrayWithArray:mutableComments], nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
    
}

+ (NSURLSessionDataTask *)addCommentByCurrentUserToTweet:(NSString *)mid WithText:(NSString *)text WithBlock:(void (^)(Comment *newComment, NSError *error)) block {
    if ([CurrentUser SharedCurrentUser].isLogin) {
        NSDictionary *params = @{@"access_token":[CurrentUser GetToken], @"id":mid, @"comment":text};
        return [[WeiboApiClient sharedClient] POST:WEIBO_API_ADD_COMMENT parameters:params success:^(NSURLSessionDataTask * __unused task, id JSON){
            Comment *newComment = [[Comment alloc] initWithAttributes:JSON];
            if (block) {
                block(newComment, nil);
            }
        } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
            Comment *newComment = [[Comment alloc] init];
            newComment.cid = @"0";
            newComment.text = text;
            User *user = [[User alloc] init];
            user.uid = [CurrentUser SharedCurrentUser].uid;
            user.screen_name = [CurrentUser SharedCurrentUser].name;
            user.profile_image_url = [CurrentUser SharedCurrentUser].avatar_url;
            newComment.owner = user;
            newComment.is_hot = [NSNumber numberWithInt:1];
            newComment.created_time = [Comment nowDateDesc];
            if (block) {
                block(newComment, error);
            }
        }];
    }
    return nil;
}

+ (NSString *)nowDateDesc {
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    return [formatter stringFromDate:date];
}


@end
